﻿package cn.antscript.airCMS.display.component 
{
	import cn.antscript.airCMS.data.UserData;
	import cn.antscript.airCMS.display.component.btnbase.AbstractIco;
	import cn.antscript.airCMS.display.component.btnbase.IcoBitmapdatas;
	import cn.antscript.model.SqlModel;
	import com.adobe.crypto.MD5;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.PushButton;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author antscript
	 */
	public class UserManagerPanel extends Sprite
	{
		private var _userPass:Label;
		//private var _userPass:PushButton;
		//private var _userAdd:PushButton;
		private var _change:PushButton;
		//private var _submit:PushButton;
		private var _close:AbstractIco;
		
		private var _oldPassLable:Label;
		private var _oldPassInput:InputText;
		private var _newPassLable:Label;
		private var _newPassInput:InputText;
		private var _rePassLable:Label;
		private var _rePassInput:InputText;
		
		private var _newUserLable:Label;
		private var _newUserInput:InputText;
		
		private static var _instance:UserManagerPanel;
		
		
		private var _sqlPanel:PushButton;
		
		public function UserManagerPanel(privateClass:PrivateClass) 
		{
			_sqlPanel=new PushButton(this,220,250,"数据库设置",onSql);
			
			_userPass = new Label(this, 0, 0, "修改密码");
			//_userPass = new PushButton(this, 0, 0, "修改密码",setChangeMode);
			//_userAdd = new PushButton(this, 104, 0, "添加管理员",setAddMode);
			_change = new PushButton(this, 220, 180, "修改",onChange);
			//_submit = new PushButton(this, 220, 180, "添加");
			_close = new AbstractIco(IcoBitmapdatas.Delete, "返回", this, 310, -5,false);
			_close.addEventListener(MouseEvent.CLICK, onClose);
			
			_oldPassLable = new Label(this, 10, 50, "密          码:");
			_oldPassInput = new InputText(this, 90, 50);
			
			//_newUserLable = new Label(this, 10, 90, "新    用   户:");
			//_newUserInput = new InputText(this, 90, 90);
			
			_newPassLable = new Label(this, 10, 140, "新    密   码:");
			_newPassInput = new InputText(this, 90, 140);
			
			_rePassLable = new Label(this, 10, 180, "重复新密码:");
			_rePassInput = new InputText(this, 90, 180);
			
			_oldPassInput.password = _newPassInput.password = _rePassInput.password =  true;
			
			//setChangeMode();
			
		}
		
		private function onSql(e:MouseEvent):void
		{
			MYSQLManager.getInstance().show(this.stage);
			MYSQLManager.getInstance().addEventListener(Event.CLOSE, onClose);
		}
		public static function getInstance():UserManagerPanel {
			if (_instance == null) {
				_instance = new UserManagerPanel(new PrivateClass);
			}
			return _instance;
		}
		
		private function onClose(e:Event):void 
		{
			hide();
		}
		private function changed(isChanged:Boolean):void {
			WaitBar.getInstance().hide();
			
			if (isChanged) {
				AlertPanel.getInstance().show("密码已修改，请牢记。", this.stage);
				UserData.PASSWORD = MD5.hash(_newPassInput.text);
			}
			else {
				AlertPanel.getInstance().show("密码修改失败，请重试。",this.stage);
			}
			
			hide();
		}
		private function onChange(e:MouseEvent):void
		{
			
			if (MD5.hash(_oldPassInput.text) != UserData.PASSWORD) {
				AlertPanel.getInstance().show("原密码错误！",this.stage);
			}
			else if (_newPassInput.text!=_rePassInput.text) {
				AlertPanel.getInstance().show("两次密码输入不一致！",this.stage);
			}
			else {
				WaitBar.getInstance().show(this.stage);
				SqlModel.getInstance().changePass(MD5.hash(_newPassInput.text), changed);
			}
			
		}
		
		public function show(container:DisplayObjectContainer):void {
			container.addChild(getInstance());
			
			this.visible = true;
			setChangeMode();
			
		}
		private function hide():void {
			
			this.visible = false;
			this.parent.removeChild(this);
		}
		
		private function setChangeMode(e:Event = null):void {
			
			graphics.clear();
			this.x = (stage.stageWidth - this.width) / 2;
			this.y = (stage.stageHeight - this.height) / 2;
			
			
			_userPass.alpha = 1;
			//_userAdd.alpha = 0.5;
			_change.visible = true;
			//_submit.visible = false;
			//_newUserLable.visible = _newUserInput.visible = false;
			
			_oldPassLable.text = "原    密   码:";
			_newPassLable.text = "新    密   码:";
			_rePassLable.text = "重复新密码:";
			
			
			graphics.lineStyle(2, 0x999999);
			graphics.beginFill(0x333333, 0.8);
			graphics.drawRect( -this.x, -this.y, stage.stageWidth, stage.stageHeight);
			graphics.endFill();
			graphics.moveTo(0, 21);
			graphics.lineTo(102,21);
			graphics.lineTo(330,21);
			graphics.lineTo(330,220);
			graphics.lineTo(0,220);
			graphics.lineTo(0, 21);
		}
		
		private function setAddMode(e:Event=null):void {
			_userPass.alpha = 0.5;
			//_userAdd.alpha = 1;
			_change.visible = false;
			//_submit.visible = true;
			_newUserLable.visible = _newUserInput.visible = true;
			
			_oldPassLable.text = "管理员密码:";
			_newPassLable.text = "密          码:";
			_rePassLable.text = "重 复 密 码:";
			
			graphics.clear();
			graphics.lineStyle(2, 0x999999);
			graphics.moveTo(0, 21);
			graphics.lineTo(104,21);
			graphics.lineTo(104,0);
			graphics.lineTo(206,0);
			graphics.lineTo(206,21);
			graphics.lineTo(330,21);
			graphics.lineTo(330,220);
			graphics.lineTo(0,220);
			graphics.lineTo(0, 21);
		}
		
	}

}
class PrivateClass {
	
}